DEFINE CLASS databaseclass as Custom

	FUNCTION abrirconeccion
	
		OPEN DATABASE 'C:\fox\Data\db_Biblioteca.dbc'
		
	ENDFUNC
	
	FUNCTION cerrarconeccion
	
		CLOSE DATABASES
		
	ENDFUNC	

ENDDEFINE

DEFINE CLASS datasocios as databaseclass 
	
	vapellido = ""
	vnombre = ""
	vemail = "" 
	vidsocio = 0
	vdomicilio = ""
	vnrosocio = 0
	vtelefono = ""
	vtipodoc  = ""
	vnrodoc = ""
	
	FUNCTION nuevo as Boolean
		exito = .F.
		this.abrirconeccion()
		
		INSERT INTO socio (apellido, nombre, email, domicilio, nrosocio,telefono, tipodoc, nrodoc);
		VALUES (this.vapellido, this.vnombre, this.vemail, this.vdomicilio, VAL(this.vnrosocio), this.vtelefono, this.vtipodoc, this.vnrodoc)
		
		IF _TALLY > 0 then
			exito = .T.
		ENDIF
		
		this.cerrarconeccion()
		RETURN exito
	ENDFUNC
	
	FUNCTION actualizar AS Boolean
		this.abrirconeccion()
		
		update socio set apellido = this.vapellido, nombre = this.vnombre, email = this.vemail, domicilio = this.vdomicilio , nrosocio = this.vnrosocio, telefono = this.vtelefono, tipodoc = this.vtipodoc, nrodoc = this.vnrodoc where idsocio = this.vidsocio
		
		this.cerrarconeccion()
		
		IF _TALLY > 0 then
		RETURN .T.
		ELSE
		RETURN .F.
		ENDIF
		
	endfunc
	
	FUNCTION delete AS Boolean
	PARAMETERS bidsocio
		exito = .F.
		delete from socio where idsocio = bidsocio
	
		IF _TALLY > 0 then
			exito = .T.
		ENDIF
		SET DELETED ON
		this.cerrarconeccion()
		RETURN exito
	ENDFUNC
	
	FUNCTION deleteall AS Boolean
		exito = .F.
		this.abrirconeccion()
		DELETE from socio 
		
		IF _TALLY > 0 then
			exito = .T.
		ENDIF
		SET DELETED ON
		this.cerrarconeccion()
	ENDFUNC
		
	FUNCTION getone as Boolean
		PARAMETERS bidsocio
		exito = .F.
		this.abrirconeccion()
		select * from socio where nrosocio = bidsocio into cursor curSocBuscado
		
		this.vapellido = curSocBuscado.apellido
		this.vnombre = curSocBuscado.nombre
		this.vemail = curSocBuscado.email
		this.vidsocio = curSocBuscado.idsocio
		this.vdomicilio = curSocBuscado.domicilio
		this.vnrosocio = curSocBuscado.nrosocio
		this.vtelefono = curSocBuscado.telefono
		this.vtipodoc = curSocBuscado.tipodoc
		this.vnrodoc = curSocBuscado.nrodoc
		
		IF _TALLY > 0 then
		exito = .T.
		ENDIF
		this.cerrarconeccion()
		RETURN exito
	ENDFUNC
	
	FUNCTION getall as CURSOR 
		exito = .F.
		this.abrirconeccion()
		SELECT * FROM socio INTO CURSOR vcursocios
		
		RETURN "SELECT nrosocio,nombre,apellido,idsocio,email,domicilio,telefono,nrodoc,tipodoc FROM socio INTO CURSOR cursocio"	
	ENDFUNC
	
	FUNCTION cantidad as int
		this.abrirconeccion()
		SET DELETED ON
		
		SELECT COUNT(*) as cant FROM socio INTO CURSOR cursocios
		n = cursocios.cant
		USE IN cursocios
		this.cerrarconeccion()
		RETURN n 
		
	ENDFUNC
	
	FUNCTION existesocio as Boolean
		PARAMETERS pnombre, papellido
		vexiste = .F.
		this.abrirconeccion()
		
		SELECT * FROM socio WHERE nombre = pnombre AND apellido = papellido INTO CURSOR curExiste
		
		IF _TALLY > 0 then
				vexiste = .T.
			ENDIF	
		this.cerrarconeccion()
		RETURN vexiste
	ENDFUNC
	
	FUNCTION getMaxId As Integer
		this.abrirconeccion()
		SELECT socio 
		CALCULATE MAX(idsocio) TO maxid
		RETURN maxid
		this.cerrarconeccion()
	ENDFUNC

ENDDEFINE

DEFINE CLASS dataautores as databaseclass
	
	vapellido = ""
	vnombre = ""
	vidautor = 0
	
	FUNCTION insert as Boolean
		
		INSERT INTO autor (apellido, nombre) ;
		VALUES (this.vapellido, this.vnombre)
		IF _Tally > 0 then
		RETURN .T.
		ELSE 
		RETURN .F.
		ENDIF
		
	ENDFUNC
	
	FUNCTION update AS Boolean
	
		update autor set ;
		apellido = this.vapellido, ;
		nombre = this.vnombre ;
		where idautor = this.vidautor
		IF _Tally > 0 then
		RETURN .T.
		ELSE 
		RETURN .F.
		ENDIF
		
	ENDFUNC
	
	FUNCTION delete AS Boolean
	
	SET DELETED OFF
	delete from autor where idautor = this.vidautor
	IF _Tally > 0 then
		RETURN .T.
		ELSE 
		RETURN .F.
		ENDIF
		
	ENDFUNC
		
	FUNCTION getone AS Boolean
		PARAMETERS bidautor

		select * from autor where idautor = bidautor into cursor curAutBuscado
		
		this.vapellido = curAutBuscado.apellido
		this.vnombre = curAutBuscado.nombre
		this.vidautor = curAutBuscado.idautor
		IF _Tally > 0 then
		RETURN .T.
		ELSE 
		RETURN .F.
		ENDIF
		
	ENDFUNC
	
	FUNCTION getall AS Boolean
		
		SELECT * FROM autor INTO CURSOR vcurautores
		
		IF _Tally > 0 then
		RETURN .T.
		ELSE 
		RETURN .F.
		ENDIF
		
	ENDFUNC
	
	FUNCTION getPrestamosPend AS Cursor
		
		SELECT libro.titulo, ejemplar.idejemplar  FROM prestamo INNER JOIN ejemplar INNER JOIN prestamoejemplar INNER JOIN libro WHERE prestamo.fechadevre<>null INTO vcurprestpend
		RETURN vcurprestpend
	
	ENDFUNC 

ENDDEFINE
